package cn.iocoder.yudao.module.squaredancing.controller.admin.version;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.squaredancing.controller.admin.version.vo.*;
import cn.iocoder.yudao.module.squaredancing.dal.dataobject.version.VersionDO;
import cn.iocoder.yudao.module.squaredancing.service.version.VersionService;

@Tag(name = "管理后台 - 版本管理")
@RestController
@RequestMapping("/squaredancing/version")
@Validated
public class VersionController {

    @Resource
    private VersionService versionService;

    @PostMapping("/create")
    @Operation(summary = "创建版本管理")
    @PreAuthorize("@ss.hasPermission('squaredancing:version:create')")
    public CommonResult<Long> createVersion(@Valid @RequestBody VersionSaveReqVO createReqVO) {
        return success(versionService.createVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新版本管理")
    @PreAuthorize("@ss.hasPermission('squaredancing:version:update')")
    public CommonResult<Boolean> updateVersion(@Valid @RequestBody VersionSaveReqVO updateReqVO) {
        versionService.updateVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除版本管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('squaredancing:version:delete')")
    public CommonResult<Boolean> deleteVersion(@RequestParam("id") Long id) {
        versionService.deleteVersion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除版本管理")
                @PreAuthorize("@ss.hasPermission('squaredancing:version:delete')")
    public CommonResult<Boolean> deleteVersionList(@RequestParam("ids") List<Long> ids) {
        versionService.deleteVersionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得版本管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('squaredancing:version:query')")
    public CommonResult<VersionRespVO> getVersion(@RequestParam("id") Long id) {
        VersionDO version = versionService.getVersion(id);
        return success(BeanUtils.toBean(version, VersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得版本管理分页")
    @PreAuthorize("@ss.hasPermission('squaredancing:version:query')")
    public CommonResult<PageResult<VersionRespVO>> getVersionPage(@Valid VersionPageReqVO pageReqVO) {
        PageResult<VersionDO> pageResult = versionService.getVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出版本管理 Excel")
    @PreAuthorize("@ss.hasPermission('squaredancing:version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVersionExcel(@Valid VersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VersionDO> list = versionService.getVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "版本管理.xls", "数据", VersionRespVO.class,
                        BeanUtils.toBean(list, VersionRespVO.class));
    }

}